<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<meta name="description" content="" />

<meta name="keywords" content="" />

<meta name="author" content="" />

<link rel="stylesheet" type="text/css" href="style.css" media="screen" />

<title>REEFPI Home Page</title>

</head>

	<body>

		<div id="wrapper">

<?php include('includes/header.php'); ?>

<?php include('includes/nav.php'); ?>

<?php include('includes/dbconnect.php'); ?>

<div id="content">

<p>Available Devices</p>
<?php
function redirect($url) {
    if(!headers_sent()) {
        //If headers not sent yet... then do php redirect
        header('Location: '.$url);
        exit;
    } else {
        //If headers are sent... do javascript redirect... if javascript disabled, do html redirect.
        echo '<script type="text/javascript">';
        echo 'window.location.href="'.$url.'";';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
        echo '</noscript>';
        exit;
    }
}

	if(isset($_POST['addDevices']))
	{
		$deviceName = ($_POST['deviceName']);
		$deviceType = ($_POST['deviceType']);
		$address = ($_POST['address']);
		$controller = ($_POST['controller']);
		$addDevice = mysqli_query($con, "INSERT INTO device ". "(deviceName, iddeviceType, address, idcontroller)". "VALUES ('$deviceName', '$deviceType', '$address', $controller)");
		$result = ($addDevice);
		if(!$result)
		{
			echo ("Sorry, cannot submit your request: " . mysqli_error($con));
		}
		else
		{
			redirect($_SERVER['PHP_SELF']);
		}
	}

	$resultAvailableDevices = mysqli_query($con, "SELECT * FROM device AS D
													INNER JOIN deviceType AS DT ON D.iddeviceType = DT.iddeviceType
													INNER JOIN controller AS C on D.idcontroller = C.idcontroller");

	echo "<table border='1'>
	<tr>
		<th>Device Name</th>
		<th>Type</th>
		<th>Address</th>
		<th>Controller</th>
		<th>Status</th>
		<th>level</th>
	</tr>";
	while($rowAvailableDevice = mysqli_fetch_array($resultAvailableDevices))
	{
		echo "<tr>";
		echo "<td>" . $rowAvailableDevice['deviceName'] . "</td>";
		echo "<td>" . $rowAvailableDevice['deviceTypeName'] . "</td>";
		echo "<td>" . $rowAvailableDevice['address'] . "</td>";
		echo "<td>" . $rowAvailableDevice['controllerName'] . "</td>";
		echo "<td>" . $rowAvailableDevice['status'] . "</td>";
		echo "<td>" . $rowAvailableDevice['level'] . "</td>";
		echo "</tr>";
	}
	echo "</table>"
?>
<BR>
<p>Add a Device</p>
<form method="post" action="<?php $_PHP_SELF ?>">
	<table width="400" border="0" cellspacing="1" cellpadding="2">
		<tr>
			<td width="100">Name</td>
			<td><input name="deviceName" type="text" id="deviceName"></td>
		</tr>
		<tr>
			<td width="100">Device Type ID</td>
			<td>
				<select name="deviceType" id="deviceType" style="width: 200px" >
   	   				<?php
   	   					$deviceTypes= mysqli_query($con, 'select * from deviceType') or die (mysql_error()); 
       
      					while($deviceType = mysqli_fetch_array($deviceTypes))
      					{
        					echo "<option value=".$deviceType['iddeviceType']."> ".$deviceType['deviceTypeName']." </option>";
      					}
      				?>
				</select>
			</td>
		</tr>
		<tr>
			<td width="100">Controller</td>
			<td>
				<select name="controller" id="controller" style="width: 200px" >
   	   				<?php
   	   					$deviceTypes= mysqli_query($con, 'select * from controller') or die (mysql_error()); 
       
      					while($deviceType = mysqli_fetch_array($deviceTypes))
      					{
        					echo "<option value=".$deviceType['idcontroller']."> ".$deviceType['controllerName']." </option>";
      					}
      				?>
				</select>
			</td>
		</tr>
		<tr>
			<td width="100">Address</td>
			<td><input name="address" type="text" id="address"></td>
		</tr>		
		<tr>
			<td width="100"> </td>
			<td>
				<input name="addDevices" type="submit" id="addDevices" value="Add Device">
			</td>
		</tr>
	</table>
</form>

</div> <!-- end #content -->

<div id="sidebar">

<?php include('includes/sidebar.php'); ?>

</div> <!-- end #sidebar -->

<div id="footer">
	<p> <a href="#">REEFPI</a></p>
</div> <!-- end #footer -->

		</div> <!-- End #wrapper -->

	</body>

</html>
